package cn.iocoder.yudao.module.ai.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 功能描述：
 *    使用操作系统执行docker命令
 */

@Slf4j
@Component
public class DockerTool {

    private static final String ROOT_PASSWORD = "Xsxx@2024^";
    private static final int INNER_PORT = 8888;
    /**
     * docker 启动命令样例：
     *   sudo  docker run -p 7249:8888
     *   -v /home/jypyter/workpath/727c17ff-75ae-41a3-b0e8-c874f1858ba7:/home/jovyan/
     *   -e JUPYTER_TOKEN='1234'
     *   -d --name jupyter_6 jupyter/datascience-notebook
     */
    private static final String DOCKER_RUN_COMMAND_TEMPLATE = "sudo  docker run -p %s:%s -v %s:/home/jovyan/ -e JUPYTER_TOKEN='1234'  -d --name %s %s";
    private static final String DOCKER_STOP_COMMAND_TEMPLATE = "echo %s | sudo -S docker stop ";

    /**
     * 运行Docker容器
     *
     * @throws IOException
     * @throws InterruptedException
     */
    public void runContainer(String containerName,String workPath,int openPort) throws IOException, InterruptedException {
        String command = String.format(DOCKER_RUN_COMMAND_TEMPLATE,openPort,INNER_PORT,workPath, containerName, "jupyter/datascience-notebook");
        log.info("启动jupyter命令为：{}",command);
        executeCommand(command);
    }

    /**
     * 停止Docker容器
     *
     * @param containerName 容器名称
     * @throws IOException
     * @throws InterruptedException
     */
    public void stopContainer(String containerName) throws IOException, InterruptedException {
        String command = String.format(DOCKER_STOP_COMMAND_TEMPLATE, ROOT_PASSWORD, containerName);
        executeCommand(command);
    }

    /**
     * 查看容器是否启动
     * @param containerName 容器名称
     * @throws IOException
     * @throws InterruptedException
     */
    public boolean chickContainerIsStart(String containerName) throws IOException, InterruptedException {
        //todo 使用docker ps -a | grep 容器名 查看容器是否启动
        String command = String.format(DOCKER_STOP_COMMAND_TEMPLATE, ROOT_PASSWORD, containerName);
        executeCommand(command);
        return true;

    }
    /**
     * 执行命令
     *
     * @param command 命令字符串
     * @throws IOException
     * @throws InterruptedException
     */
    private void executeCommand(String command) throws IOException, InterruptedException {
        ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c", command);
        processBuilder.redirectErrorStream(true); // 合并错误流和标准输出流

        Process process = processBuilder.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

        int exitCode = process.waitFor();
        if (exitCode != 0) {
            throw new RuntimeException("Command failed with exit code: " + exitCode);
        }
    }



}